#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

'''docstring
'''

__revision__ = '0.1'
__author__ = 'lxd'
import wx
import re
import tools
import win32con

def getLblAndTxt(parent,label,is_pwd = False):
    lbl= wx.StaticText(parent, -1, label)
    if is_pwd:
        txt= wx.TextCtrl(parent, -1, '',style = wx.TE_PASSWORD)  
    else:
        txt= wx.TextCtrl(parent, -1, '')
    return lbl,txt

def getHotkeyControl(parent, hotkey1='', hotkey2='', label=u'设置快捷键'):
    choice1 = ['Alt', 'Ctrl','Shift','Win']
    choice2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11','F12']
    lblHotkey = wx.StaticText(parent, -1, label)
    chHotkey1 = wx.Choice(parent, -1, choices=choice1)
    chHotkey2 = wx.Choice(parent, -1, choices=choice2)
    index = chHotkey1.FindString(hotkey1)
    chHotkey1.SetSelection(index)
    index = chHotkey2.FindString(hotkey2)
    chHotkey2.SetSelection(index)
    return lblHotkey, chHotkey1, chHotkey2

def getHotkey(hotkey1, hotkey2):
    if hotkey1 == 'Alt':
        choice1 =  win32con.MOD_ALT
    elif hotkey1 ==  'Ctrl':
        choice1 = win32con.MOD_CONTROL
    elif hotkey1 ==  'Shift':
        choice1 = win32con.MOD_SHIFT
    elif hotkey1 ==  'Win':
        choice1 = win32con.MOD_WIN

    if hotkey2.startswith('F'):
        choice2 = 111 + int(hotkey2[1:])#VK_F1 -> 112
    else:
        choice2 = ord(hotkey2.upper())
    return choice1, choice2

def msg(str):
    wx.MessageBox(str,u'友情提示',style = wx.OK|wx.ICON_EXCLAMATION)

def confirm(str):
    """result = wx.ID_YES or wx.ID_NO or wx.ID_CANCEL 
    """
    dlg = wx.MessageDialog(None, str, u'友情提示', wx.YES|wx.NO|wx.CANCEL|wx.ICON_QUESTION)
    result = dlg.ShowModal()
    dlg.Destroy()
    return result

def isBlank(txtControl):
    if txtControl.GetLabel().strip() == '':
        msg(u'带星号的不得为空')
        txtControl.SetFocus()
        return True
    else:
        return False

def isNotEqual(txtControl1,txtControl2):
    if txtControl1.GetLabel().strip() != txtControl2.GetLabel().strip():
        msg(u'两次密码必须一样')
        txtControl1.SetLabel('')
        txtControl2.SetLabel('')
        txtControl1.SetFocus()
        return True
    else:
        return False

def isNotEmail(txtEmail):
    p = re.compile('\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*')
    m = p.search(txtEmail.GetLabel().strip())
    if m:
        return False
    else:
        msg(u'请输入有效的邮箱')
        txtEmail.SetLabel('')
        txtEmail.SetFocus()
        return True

class MyListCtrl(wx.ListCtrl):
    """传的值都为数组, 自动转化为str类型传入传出
    """
    def insertItem(self, index, values):
        """自动转化为str，然后再插入
        """
        self.InsertStringItem(index, tools.string(values[0]))
        for i in range(1, len(values)):
            self.SetStringItem(index, i, tools.string(values[i]))

    def appendItem(self, values): 
        index = self.GetItemCount()
        self.insertItem(index, values)  

    def appendItems(self, values):
        for value in values:
            self.appendItem(value)
            
    def updateItem(self, index, values):
        """自动转化为str
        """
        old_values = self.getItemValues(index)
        assert len(old_values) == len(values)
        for i in range(len(old_values)):
            if tools.string(values[i]) != old_values[i]:
                self.SetStringItem(index, i, tools.string(values[i])) 

    def getItemValues(self, index):
        """获得某行的值, 全为str类型
        """
        columnCount = self.GetColumnCount()
        values = []
        for i in range(columnCount):
            values.append(self.GetItem(index, i).Text)  
        return values               

    def getItemsValues(self):    
        """获得所有行的值
        """
        count = self.GetItemCount()
        columnCount = self.GetColumnCount()
        items = []
        for i in range(count): 
            values = []
            for j in range(columnCount): 
                values.append(self.GetItem(i, j).Text)
            items.append(values)        
        return items        

##########实现了删除，上移，下移##########
    def OnDelClick(self, event):   
        index = self.GetFirstSelected()
        self.DeleteItem(index)

    def OnUpClick(self, event):
        index = self.GetFirstSelected()
        if index > 0:
            values = self.getItemValues(index)
            self.DeleteItem(index)
            self.insertItem(index - 1, values)
            self.Select(index - 1)

    def OnDownClick(self, event): 
        index = self.GetFirstSelected()
        if index < self.GetItemCount() - 1:
            values = self.getItemValues(index)
            self.DeleteItem(index)
            self.insertItem(index + 1, values)
            self.Select(index + 1)

if __name__ == '__main__':
    pass

